home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / EnterAct 3.5 / Drag_on Modules / hAWK programs / $WordFrequency < prev    next >
Encoding:
Text File  |  1994-05-14  |  2.3 KB  |  72 lines  |  [TEXT/KEEN]

  1. #$WordFrequency: print sorted list of words, together with number
  2. #of times each word occurs. Use with any input option, 
  3. #select "Show stdout" since that's where the output goes.
  4.  
  5. #The file "common words" in the "hAWK programs" folder contains
  6. #a list of words to skip. To do a better job, you can create a
  7. #custom list - for example, the word "while" can be skipped in
  8. #ordinary text, but should be included if the text deals with
  9. #C or hAWK programming. If this file is missing, the program
  10. #will still run, but common words will not be skipped (this
  11. #uses a lot more memory).
  12. #
  13. #This isn't perfect, but is very useful as-is. It's a simple
  14. #program, one you can tinker with easily - try it out on
  15. #some small files, and refinements will
  16. #suggest themselves.
  17. #
  18. # User’s Manual references:
  19. # «hAWK User’s Manual» «F   Running hAWK programs»
  20. # «hAWK User’s Manual» «L  5   Regular expressions»
  21. # «hAWK User’s Manual» «M  5   Built-in string and file functions»
  22. # «hAWK User’s Manual» «K  4   Built-in variables»
  23. # «hAWK User’s Manual» «K  8   Arrays»
  24. # «hAWK User’s Manual» «N   User-defined functions»
  25. # «hAWK User’s Manual» «P  3   The getline function»
  26. # «hAWK User’s Manual» «O  3   Output into files»
  27. # «hAWK User’s Manual» «Q   The hAWK function»
  28.  
  29. BEGIN { #Get list of common words to skip.
  30.     commonfile = STDPATH "Drag_on Modules:hAWK programs:" "common words"
  31.     while (getline < commonfile > 0)
  32.         {
  33.         for ( k = 1; k <= NF; k++)
  34.             common[$k] = 1; #Forces common[$k] to "exist".
  35.         }
  36.     close(commonfile)
  37.     $0 = ""
  38.     ## time_it = 1
  39.     if (time_it == 1)
  40.         print "Starting time", time()
  41.     }
  42.  
  43. #If you're debugging a hAWK program, you might want to
  44. #simplify output by uncommenting the pattern-action below
  45. #and saving this as "$hAWKWordFrequency" or somesuch.
  46.  
  47. ##    /^#/ {next} #skip lines containing hAWK comments
  48.  
  49.     { #Remove non-word characters, count words.
  50.       gsub(/[^A-Za-z_0-9$'-]+/, " ")
  51.       #or try gsub(/\W+/, " ") #W == [^A-Z_a-z0-9]
  52.       for ( k = 1; k <= NF; k++)
  53.           {
  54.         if (length($k) > 1 && !($k in common))
  55.               count[$k]++;
  56.         }
  57.     total_words += NF;
  58.     }
  59. END { #Sort associative array, and print words with count.
  60.       for (w in count)
  61.           {
  62.         linear[++m] = w "\t\t" count[w]
  63.         }
  64.       sort(linear, ind, "d")
  65.       for (j = 1; j <= m; ++j)
  66.           print linear[ind[j]]
  67.       print "Total words:", total_words;
  68.       if (time_it == 1)
  69.           print "Finishing time", time()
  70.     }
  71.  
  72.